//
//  SHMorePicController.m
//  UI_CarHome
//
//  Created by dllo on 15/11/4.
//  Copyright (c) 2015年 沈浩. All rights reserved.
//

#import "SHMorePicController.h"
#import "SHHandle.h"
#import "SHPicCollectionViewCell.h"
#import "SHRollimgeViewController.h"
@interface SHMorePicController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)UICollectionView *moreCollectionView;
@property(nonatomic, retain)NSMutableArray *photosArr;
@property(nonatomic, retain)NSDictionary *dic;
@property(nonatomic, copy)NSString *url;
@property(nonatomic, retain)UILabel *noMorePicLabel;
@end

@implementation SHMorePicController
- (void)dealloc
{
    [_dic release];
    [_url release];
    [_noMorePicLabel release];
    [_moreCollectionView release];
    [_photosArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BACKGROUNDCOLOR;
    self.title = self.typeTitle;
    [self createData];
    [self.view addSubview:self.moreCollectionView];
    [_moreCollectionView release];

}
#pragma mark -- createData
- (void)createData {
    if (self.noMorePicLabel) {
        [self.noMorePicLabel removeFromSuperview];
    }
    self.url = [NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v2/price/picturesv47?v=4.7.0&serialGroupId=%@&modelId=null&typeId=%ld&colorId=&pageSize=9999&pageNo=1", self.myId, self.typeId];
    [SHNetWorkingTool getNetWorking:self.url block:^(id result) {
        
        if ([result[@"pageCount"] isEqualToString:@"0"]) {
            self.noMorePicLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -64, WIDTH, HEIGHT)];
            self.noMorePicLabel.text = @"没有更多的相关图片了!";
            self.noMorePicLabel.textAlignment = NSTextAlignmentCenter;
            [self.moreCollectionView addSubview:self.noMorePicLabel];
            [_noMorePicLabel release];
        } else {
            self.photosArr = [NSMutableArray array];
            self.photosArr = result[@"sections"][0][@"photos"];

        }
        [self.moreCollectionView reloadData];
    }];
    
}
#pragma mark --collectionView点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    SHRollimgeViewController *rollImage = [[SHRollimgeViewController alloc] init];
    [self.navigationController pushViewController:rollImage animated:YES];
    rollImage.index = indexPath.row;
    rollImage.phoArr = self.photosArr;
    rollImage.shareIamge = self.photosArr[indexPath.row][@"bigPath"];
    rollImage.webStr = self.url;
    [rollImage release];
}


#pragma mark --collectionView
- (UICollectionView *)moreCollectionView {
    if (!_moreCollectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(100, 70);
        flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        self.moreCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowLayout];
        self.moreCollectionView.dataSource = self;
        self.moreCollectionView.delegate = self;
        
        self.moreCollectionView.backgroundColor = BACKGROUNDCOLOR;
        [self.moreCollectionView registerClass:[SHPicCollectionViewCell class] forCellWithReuseIdentifier:@"morePic"];
    }
    return _moreCollectionView;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SHPicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"morePic" forIndexPath:indexPath];
    cell.photosDic = self.photosArr[indexPath.row];
    
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.photosArr.count;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
